package org.zhouhe.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.springframework.stereotype.Service;
import org.zhouhe.entity.TSysUser;

@Service
public class UserService extends BaseService {
	public Map<String, Object> findUserDetail(Long userId){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", userId);
		return this.selectOne("T_SYS_USER.selectUserDetail", map);
	}
	
	public List<Map<String, Object>> findUserList(){
		Map<String, Object> map = new HashMap<String, Object>();
		return this.selectList("T_SYS_USER.selectUserList", map);
	}
	
	
	/**
	 * 测试Hibernate事务
	 * @作者： zhouhe
	 */
	public void saveX(){
		TSysUser u = new TSysUser();
		u.setLoginName("zhouhe");
		u.setRealName("周鹤");
		u.setPassword("111111");
		this.save(u);
		System.out.println("===========================error");
		if(true)
			throw new RuntimeException("抛出异常，事务需要rollback");
		TSysUser u1 = new TSysUser();
		u1.setLoginName("zhouhe");
		u1.setRealName("周鹤");
		u1.setPassword("111112");
		this.save(u1);
	}

	/**
	 * 测试MyBatis事务
	 * @作者： zhouhe
	 */
	public void saveY(){
		Map<String, String> map = new HashMap<String, String>();
		map.put("loginName", "张三");
		this.execute("T_SYS_USER.testIns", map);
		if(true)
			throw new RuntimeException("抛出异常，事务需要rollback");
		map.put("loginName", "李四");
		this.execute("T_SYS_USER.testIns", map);
	}

	/**
	 * 测试事务共享（1）
	 * @作者： zhouhe
	 */
	public void saveZ(){
		TSysUser u = new TSysUser();
		u.setLoginName("TEST TRAN");
		u.setRealName("共享事务测试");
		this.save(u);

		//MyBatis应该能读取到Hibernate刚刚插入的记录（事务还未提交）
		List<Map<String,Object>> list = this.selectList("T_SYS_USER.selectUserList", null);
		for(Map<String,Object> map : list){
			System.out.println(">>"+ map.get("LOGIN_NAME") +"\t"+ map.get("REAL_NAME"));
		}
		if(true)
			throw new RuntimeException("抛出异常，事务需要rollback");
	}

	/**
	 * 测试事务共享（2）
	 * 
	 * @作者： zhouhe
	 */
	public void saveZ2(){
		Map<String, String> map = new HashMap<String, String>();
		map.put("loginName", "小习");
		this.execute("T_SYS_USER.testIns", map);
		
		//Hibernate应该能读取到MyBatis刚刚插入的记录（事务还未提交）
		TSysUser u = this.get(TSysUser.class, 10);
		System.out.println("================================================");
		System.out.println(ReflectionToStringBuilder.toString(u));

		if(true)
			throw new RuntimeException("抛出异常，事务需要rollback");
	}
	
	public TSysUser getUser(){
		return this.get(TSysUser.class, 1);
	}
}
